package com.hr;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Index;
import org.hibernate.annotations.Table;

import com.espirit.EntityMapping;
import com.espirit.common.VersionAware;
import com.espirit.eap.annotation.Dict;
import com.espirit.eap.annotation.DomainModel;
import com.espirit.eap.sa.User;
import com.espirit.finance.core.AcctTerm;
import com.espirit.finance.core.AcctTermCheckAware;
import com.googlecode.cswish.model.Saas;

@Entity
@DomainModel
@Table(indexes={
		@Index(name="idx_accttermstate", columnNames={"acctterm_id","state"} )},appliesTo = "hr_bankcollection")
public class BankCollection extends EntityMapping implements Serializable , VersionAware, AcctTermCheckAware, Saas{

	public static final String  STATE = "bankcollection_state";
	public static final Integer STATE_NORMAL = 0;
	public static final Integer STATE_SUCCEED = 1;
	public static final Integer STATE_FAILED= 2;
	
	private static final long serialVersionUID = -6311921494845675882L;
	
	private Integer sid;
	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	public AcctTerm getAcctTerm() {
		return acctTerm;
	}
	public void setAcctTerm(AcctTerm acctTerm) {
		this.acctTerm = acctTerm;
	}
	public Integer getItemCounts() {
		return itemCounts;
	}
	public void setItemCounts(Integer itemCounts) {
		this.itemCounts = itemCounts;
	}
	public BigDecimal getAmount() {
		return amount;
	}
	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}
	
	@Dict(type=STATE)
	public Integer getState() {
		return state;
	}
	public void setState(Integer state) {
		this.state = state;
	}

	public Date getCreatedOn() {
		return createdOn;
	}
	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	public User getCreated() {
		return created;
	}
	public void setCreated(User created) {
		this.created = created;
	}


	@OneToMany(targetEntity = BankCollectionItem.class ,orphanRemoval = true, fetch=FetchType.LAZY,  cascade = CascadeType.ALL, mappedBy = "parent")
	public List<BankCollectionItem> getChildren() {
		return children;
	}
	public void setChildren(List<BankCollectionItem> children) {
		this.children = children;
	}
	
	public List<BankCollectionItem> safeChildren(){
		if (children == null){
			children = new ArrayList<BankCollectionItem>();
		}
		return children;
	}

	private AcctTerm acctTerm;
	private Integer itemCounts;
	private BigDecimal amount;
	private Integer state;
	private Date createdOn;
	

	private User created;
	private List<BankCollectionItem> children;

	@Override
	public boolean check(AcctTerm acctTerm) {
		if (acctTerm != null && acctTerm.getBeginDate() != null && this.acctTerm != null){
			return acctTerm.getName().compareTo(this.acctTerm.getName()) <= 0;
		} else {
			return true;
		}
	}
}
